package com.zabra.zl.ui.updatepwd_islogin;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.ui.updatepwd_islogin.contract.UpdatePwdIsLoginContract;
import com.zabra.zl.ui.updatepwd_islogin.presenter.UpdatePwdIsLoginPresenter;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UpdatePwdIsLoginActivity extends BaseMvpActivity<UpdatePwdIsLoginPresenter> implements UpdatePwdIsLoginContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_newpwd)
    EditText etNewpwd;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.bt_sure)
    TextView btSure;
    private Dialog dialog;
    private String strTel;

    @Override
    public int getLayoutId() {
        return R.layout.activity_update_pwd_is_login;
    }

    @Override
    public void initView() {
        tvTitle.setText("修改登录密码");
        strTel = getIntent().getExtras().getString("tel");
        mPresenter = new UpdatePwdIsLoginPresenter();
        mPresenter.attachView(this);

    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this,bean.getRespDesc());
        }
    }


    @OnClick({R.id.back, R.id.bt_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.bt_sure:
                String trim = etNewpwd.getText().toString().trim();
                String trim1 = etPwd.getText().toString().trim();
                if (TextUtils.isEmpty(trim) || TextUtils.isEmpty(trim1)) {
                    ToastUtil.showToast(this, "请输入密码");
                } else {
                    if (trim.length() < 8) {
                        ToastUtil.showToast(this, "请输入至少8位密码");
                    } else {
                        if (!trim.equals(trim1)) {
                            ToastUtil.showToast(this, "两次密码不一致，请重新输入");
                        } else {
                            keyList.add("phone");
                            keyList.add("pwd");
                            valueList.add(strTel);
                            valueList.add(trim);
                            mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));
                            keyList.clear();
                            valueList.clear();
                        }
                    }
                }
                break;
        }
    }
}
